package chenjiuxu.seniorui.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import chenjiuxu.seniorui.R;

/**
 * Created by 15705 on 2017/12/6.
 * 瀑布流布局
 */
public class FallsLayout extends ViewGroup {
    private final float spacing;
    private final int column;
    int measuredWidth = 0;//宽
    int measuredHeight = 0;//高


    public FallsLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.FallsLayout);
        spacing = typedArray.getDimension(R.styleable.FallsLayout_fl_spacing, 20);
        column = typedArray.getInt(R.styleable.FallsLayout_fl_column, 2);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (measuredWidth != 0 && measuredHeight != 0) {
            setMeasuredDimension(measuredWidth, measuredHeight);
            return;
        }
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int columnWSize = (int) (widthSize - (column - 1) * spacing) / column;//每列宽度
        widthMeasureSpec = MeasureSpec.makeMeasureSpec(columnWSize, MeasureSpec.EXACTLY);
        int viewCount = getChildCount();
        int[] h = new int[column];
        int w = 0;
        for (int i = 0; i < viewCount; i++) {
            View view = getChildAt(i);
            measureChild(view, widthMeasureSpec, heightMeasureSpec);
            int width = columnWSize;//获取测量后高度
            int height = view.getMeasuredHeight();//获取
            FallsLayoutParams fallsParams = (FallsLayoutParams) view.getLayoutParams();
            int yu = i % column;
            if (yu == column - 1) {//换行
                fallsParams.left = w;
                fallsParams.top = h[yu];
                fallsParams.right = w + width;
                fallsParams.bottom = h[yu] + height;
                w += width;
                measuredWidth = Math.max(w, measuredWidth);
                w = 0;
            } else {
                fallsParams.left = w;
                fallsParams.top = h[yu];
                fallsParams.right = w + width;
                fallsParams.bottom = h[yu] + height;
                w += width + spacing;
                if (i == viewCount - 1) measuredWidth = Math.max(w, measuredWidth);
            }
            h[yu] += height + spacing;//累加每一列的高
        }
        measuredHeight = getMax(h);//选择最高列
        setMeasuredDimension(measuredWidth, measuredHeight);
    }

    private int getMax(int[] h) {
        int tag = 0;
        for (int i : h) tag = Math.max(tag, i);
        return tag;
    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = this.getChildAt(i);
            FallsLayoutParams lParams = (FallsLayoutParams) child.getLayoutParams();
            child.layout(lParams.left, lParams.top, lParams.right, lParams.bottom);
        }
    }


//子view LayoutParams 是xml 调用父容器 generateLayoutParams（）方法生成

    @Override
    public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new FallsLayoutParams(getContext(), attrs);
    }

    @Override
    protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
        return new FallsLayoutParams(FallsLayoutParams.WRAP_CONTENT, FallsLayoutParams.WRAP_CONTENT);
    }

    @Override
    protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
        return new FallsLayoutParams(p);
    }

    @Override
    protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
        return p instanceof FallsLayoutParams;
    }

    /**
     * 自定义LayoutParams 保存布局数据
     */
    public static class FallsLayoutParams extends ViewGroup.LayoutParams {
        public int left = 0;
        public int top = 0;
        public int right = 0;
        public int bottom = 0;

        public FallsLayoutParams(Context c, AttributeSet attrs) {
            super(c, attrs);
        }

        public FallsLayoutParams(int width, int height) {
            super(width, height);
        }

        public FallsLayoutParams(LayoutParams source) {
            super(source);
        }
    }


}
